package com.st.buy.manager.controller;

import com.st.buy.pojo.TbContentCategory;
import com.st.buy.util.pojo.BuyResult;
import com.st.buy.util.pojo.EasyUITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 张双虎
 */
@RestController
public class ContentCategoryController {


    @Autowired
    private RestTemplate restTemplate;

    @Value("${contentService}")
    private String contentService;

    @RequestMapping("/content/category/create")
    public BuyResult save(TbContentCategory category){
        //远程调用content服务进行数据保存
       return restTemplate.postForObject("http://"+contentService+"/contentCategory/save", category, BuyResult.class);
    }

    @RequestMapping("/content/category/update")
    public String updateCat(TbContentCategory category){
        return restTemplate.postForObject("http://"+contentService+"/contentCategory/update", category, String.class);
    }

    @RequestMapping("/content/category/delete/")
    public String deleteCat(long id){
        restTemplate.delete("http://"+contentService+"/contentCategory/delete?catId="+id);
        return "ok";
    }

    @RequestMapping("/content/category/list")
    public List<EasyUITreeNode> queryCats(@RequestParam(value="id",defaultValue = "0") long pid){
        return restTemplate.getForObject("http://"+contentService+"/contentCategory/list?pid="+pid,List.class);
    }
}
